#include "shaderFractal.h"

#include "shader.h"

namespace fractals {
	using namespace boost;

	ShaderFractal::ShaderFractal(const char *filename)
		: minX(-2), maxX(2), minY(-2), maxY(2)
	{
		string error;

		error=s.attachVertexSource("void main(){ gl_Position=ftransform(); gl_TexCoord[0]=gl_MultiTexCoord0; }");
		check(error);

		string fragmentSource=readFile(filename);
		error=s.attachFragmentSource(fragmentSource.c_str());
		check(error);

		error=s.link();
		check(error);
	}

	void ShaderFractal::draw(bool &didClear) {
		use(shader()) {
			glClear(GL_COLOR_BUFFER_BIT);
			didClear=true;

			glBegin(GL_QUADS);
			glTexCoord2f(minX, minY);
			glVertex2f(minX, minY);

			glTexCoord2f(minX, maxY);
			glVertex2f(minX, maxY);

			glTexCoord2f(maxX, maxY);
			glVertex2f(maxX, maxY);

			glTexCoord2f(maxX, minY);
			glVertex2f(maxX, minY);
			glEnd();
		}
	}

	void ShaderFractal::resize(unsigned int w, unsigned int h) {
		p=centerFitRectagle(w, h, minX, maxX, minY, maxY);
	}
}